<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Efficient insertion</title>
<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="../container.html" title="Chapter&#160;7.&#160;Boost.Container">
<link rel="prev" href="../container.html" title="Chapter&#160;7.&#160;Boost.Container">
<link rel="next" href="containers_of_incomplete_types.html" title="Containers of Incomplete Types">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
<td align="center"><a href="../../../index.html">Home</a></td>
<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../container.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../container.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="containers_of_incomplete_types.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="container.move_emplace"></a><a class="link" href="move_emplace.html" title="Efficient insertion">Efficient insertion</a>
</h2></div></div></div>
<div class="toc"><dl>
<dt><span class="section"><a href="move_emplace.html#container.move_emplace.move_containers">Move-aware containers</a></span></dt>
<dt><span class="section"><a href="move_emplace.html#container.move_emplace.emplace">Emplace: Placement insertion</a></span></dt>
</dl></div>
<p>
      Move semantics and placement insertion are two features brought by C++11 containers
      that can have a very positive impact in your C++ applications. Boost.Container
      implements both techniques both for C++11 and C++03 compilers.
    </p>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="container.move_emplace.move_containers"></a><a class="link" href="move_emplace.html#container.move_emplace.move_containers" title="Move-aware containers">Move-aware containers</a>
</h3></div></div></div>
<p>
        All containers offered by <span class="bold"><strong>Boost.Container</strong></span>
        can store movable-only types and actual requirements for <code class="computeroutput"><span class="identifier">value_type</span></code>
        depend on each container operations. Following C++11 requirements even for
        C++03 compilers, many operations now require movable or default constructible
        types instead of just copy constructible types.
      </p>
<p>
        Containers themselves are also movable, with no-throw guarantee if allocator
        or predicate (if present) copy operations are no-throw. This allows high
        performance operations when transferring data between vectors. Let's see
        an example:
      </p>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">container</span><span class="special">/</span><span class="identifier">vector</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">move</span><span class="special">/</span><span class="identifier">utility</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>

<span class="comment">//Non-copyable class</span>
<span class="keyword">class</span> <span class="identifier">non_copyable</span>
<span class="special">{</span>
   <span class="identifier">BOOST_MOVABLE_BUT_NOT_COPYABLE</span><span class="special">(</span><span class="identifier">non_copyable</span><span class="special">)</span>

   <span class="keyword">public</span><span class="special">:</span>
   <span class="identifier">non_copyable</span><span class="special">(){}</span>
   <span class="identifier">non_copyable</span><span class="special">(</span><span class="identifier">BOOST_RV_REF</span><span class="special">(</span><span class="identifier">non_copyable</span><span class="special">))</span> <span class="special">{}</span>
   <span class="identifier">non_copyable</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span><span class="identifier">BOOST_RV_REF</span><span class="special">(</span><span class="identifier">non_copyable</span><span class="special">))</span> <span class="special">{</span> <span class="keyword">return</span> <span class="special">*</span><span class="keyword">this</span><span class="special">;</span> <span class="special">}</span>
<span class="special">};</span>

<span class="keyword">int</span> <span class="identifier">main</span> <span class="special">()</span>
<span class="special">{</span>
   <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">container</span><span class="special">;</span>

   <span class="comment">//Store non-copyable objects in a vector</span>
   <span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">non_copyable</span><span class="special">&gt;</span> <span class="identifier">v</span><span class="special">;</span>
   <span class="identifier">non_copyable</span> <span class="identifier">nc</span><span class="special">;</span>
   <span class="identifier">v</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">nc</span><span class="special">));</span>
   <span class="identifier">assert</span><span class="special">(</span><span class="identifier">v</span><span class="special">.</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="number">1</span><span class="special">);</span>

   <span class="comment">//Reserve no longer needs copy-constructible</span>
   <span class="identifier">v</span><span class="special">.</span><span class="identifier">reserve</span><span class="special">(</span><span class="number">100</span><span class="special">);</span>
   <span class="identifier">assert</span><span class="special">(</span><span class="identifier">v</span><span class="special">.</span><span class="identifier">capacity</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="number">100</span><span class="special">);</span>

   <span class="comment">//This resize overload only needs movable and default constructible</span>
   <span class="identifier">v</span><span class="special">.</span><span class="identifier">resize</span><span class="special">(</span><span class="number">200</span><span class="special">);</span>
   <span class="identifier">assert</span><span class="special">(</span><span class="identifier">v</span><span class="special">.</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="number">200</span><span class="special">);</span>

   <span class="comment">//Containers are also movable</span>
   <span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">non_copyable</span><span class="special">&gt;</span> <span class="identifier">v_other</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">v</span><span class="special">));</span>
   <span class="identifier">assert</span><span class="special">(</span><span class="identifier">v_other</span><span class="special">.</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="number">200</span><span class="special">);</span>
   <span class="identifier">assert</span><span class="special">(</span><span class="identifier">v</span><span class="special">.</span><span class="identifier">empty</span><span class="special">());</span>

   <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="container.move_emplace.emplace"></a><a class="link" href="move_emplace.html#container.move_emplace.emplace" title="Emplace: Placement insertion">Emplace: Placement insertion</a>
</h3></div></div></div>
<p>
        All containers offered by <span class="bold"><strong>Boost.Container</strong></span>
        implement placement insertion, which means that objects can be built directly
        into the container from user arguments without creating any temporary object.
        For compilers without variadic templates support placement insertion is emulated
        up to a finite (10) number of arguments.
      </p>
<p>
        Expensive to move types are perfect candidates emplace functions and in case
        of node containers (<code class="computeroutput"><a class="link" href="../boost/container/list.html" title="Class template list">list</a></code>,
        <code class="computeroutput"><a class="link" href="../boost/container/set.html" title="Class template set">set</a></code>, ...) emplace allows
        storing non-movable and non-copyable types in containers! Let's see an example:
      </p>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">container</span><span class="special">/</span><span class="identifier">list</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>

<span class="comment">//Non-copyable and non-movable class</span>
<span class="keyword">class</span> <span class="identifier">non_copy_movable</span>
<span class="special">{</span>
   <span class="identifier">non_copy_movable</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">non_copy_movable</span> <span class="special">&amp;);</span>
   <span class="identifier">non_copy_movable</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span><span class="keyword">const</span> <span class="identifier">non_copy_movable</span> <span class="special">&amp;);</span>

   <span class="keyword">public</span><span class="special">:</span>
   <span class="identifier">non_copy_movable</span><span class="special">(</span><span class="keyword">int</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span> <span class="special">{}</span>
<span class="special">};</span>

<span class="keyword">int</span> <span class="identifier">main</span> <span class="special">()</span>
<span class="special">{</span>
   <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">container</span><span class="special">;</span>

   <span class="comment">//Store non-copyable and non-movable objects in a list</span>
   <span class="identifier">list</span><span class="special">&lt;</span><span class="identifier">non_copy_movable</span><span class="special">&gt;</span> <span class="identifier">l</span><span class="special">;</span>
   <span class="identifier">non_copy_movable</span> <span class="identifier">ncm</span><span class="special">;</span>

   <span class="comment">//A new element will be built calling non_copy_movable(int) contructor</span>
   <span class="identifier">l</span><span class="special">.</span><span class="identifier">emplace</span><span class="special">(</span><span class="identifier">l</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="number">0</span><span class="special">);</span>
   <span class="identifier">assert</span><span class="special">(</span><span class="identifier">l</span><span class="special">.</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="number">1</span><span class="special">);</span>

   <span class="comment">//A new element will be built calling the default constructor</span>
   <span class="identifier">l</span><span class="special">.</span><span class="identifier">emplace</span><span class="special">(</span><span class="identifier">l</span><span class="special">.</span><span class="identifier">begin</span><span class="special">());</span>
   <span class="identifier">assert</span><span class="special">(</span><span class="identifier">l</span><span class="special">.</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="number">2</span><span class="special">);</span>
   <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      </p>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2009-2012 Ion
      Gaztanaga<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../container.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../container.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="containers_of_incomplete_types.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
